﻿using DevExpress.Web.ASPxEditors;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.Mvc;
using System.Web.Mvc;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DevExpress.Web.Demos {
    public partial class ExportingController: DemoController {
        public ActionResult ExportDetails() {
            return DemoView("ExportDetails", NorthwindDataProvider.GetCategories());
        }
        public ActionResult ExportDetailsTo(GridViewDetailExportMode exportMode) {
            foreach(string typeName in GridViewExportDemoHelper.ExportTypes.Keys) {
                if(Request.Params[typeName] != null) {
                    GridViewSettings masterGridSettings = GridViewExportDemoHelper.CreateGeneralMasterGridSettings(exportMode);
                    return GridViewExportDemoHelper.ExportTypes[typeName].Method(masterGridSettings, NorthwindDataProvider.GetCategories());
                }
            }
            return RedirectToAction("ExportDetails");
        }
        public ActionResult ExportDetailsMasterPartial() {
            return PartialView("ExportDetailsMasterPartial", NorthwindDataProvider.GetCategories());
        }
        public ActionResult ExportDetailsDetailPartial(int categoryID) {
            ViewBag.CategoryID = categoryID;
            return PartialView("ExportDetailsDetailPartial", NorthwindDataProvider.GetProducts(categoryID));
        }
    }

     public partial class GridViewExportDemoHelper {
        public static GridViewSettings CreateGeneralMasterGridSettings() {
            return CreateGeneralMasterGridSettings(ASPxGridView.GridViewDetailExportMode.None);
        }
        public static GridViewSettings CreateGeneralMasterGridSettings(ASPxGridView.GridViewDetailExportMode exportMode) {
            GridViewSettings settings = new GridViewSettings();
            settings.Name = "masterGrid";
            settings.Width = Unit.Percentage(100);

            settings.KeyFieldName = "CategoryID";
            settings.Columns.Add("CategoryID");
            settings.Columns.Add("CategoryName");
            settings.Columns.Add("Description");
            settings.Columns.Add(c => {
                c.FieldName = "Picture";
                c.ColumnType = MVCxGridViewColumnType.BinaryImage;
                BinaryImageEditProperties properties = (BinaryImageEditProperties)c.PropertiesEdit;
                properties.ImageWidth = 120;
                properties.ImageHeight = 80;
                properties.ExportImageSettings.Width = 90;
                properties.ExportImageSettings.Height = 60;
            });

            settings.SettingsDetail.ShowDetailRow = true;
            settings.SettingsDetail.ExportMode = exportMode;

            settings.SettingsExport.GetExportDetailGridViews = (s, e) => {
                int categoryID = (int)DataBinder.Eval(e.DataItem, "CategoryID");
                GridViewExtension grid = new GridViewExtension(CreateGeneralDetailGridSettings(categoryID));
                grid.Bind(NorthwindDataProvider.GetProducts(categoryID));
                e.DetailGridViews.Add(grid);
            };

            return settings;
        }

        public static GridViewSettings CreateGeneralDetailGridSettings(int uniqueKey) {
            GridViewSettings settings = new GridViewSettings();
            settings.Name = "detailGrid" + uniqueKey;
            settings.Width = Unit.Percentage(100);

            settings.KeyFieldName = "ProductID";
            settings.Columns.Add("ProductID");
            settings.Columns.Add("ProductName");
            settings.Columns.Add("UnitPrice");
            settings.Columns.Add("QuantityPerUnit");

            settings.SettingsDetail.MasterGridName = "grid";

            return settings;
        }
    }
}
